{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "大家好！今天我来教大家通过新浪微博api抓微博评论，并生成评论关系图，看看哪个键盘侠嘴最臭！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据抓取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T14:24:53.467677Z",
     "start_time": "2020-05-12T14:24:52.802377Z"
    }
   },
   "outputs": [],
   "source": [
    "#抓取的包\n",
    "import urllib\n",
    "import urllib.request\n",
    "\n",
    "#json文件解析\n",
    "import json\n",
    "\n",
    "#数据处理包\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T14:38:29.811881Z",
     "start_time": "2020-05-12T14:38:29.807893Z"
    }
   },
   "source": [
    "首先，各位在用微博api之前，需要申请一下成为微博开发者：[微博开放平台](https://open.weibo.com/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "然后，新建一个应用，随便什么应用都可以，应用不需要审核，只需要创建就行"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "今天我们要用的api接口是这个：[根据微博ID返回某条微博的评论列表](https://open.weibo.com/wiki/2/comments/show)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "然后打开:[api测试工具](http://open.weibo.com/tools/console?uri=comments/show&amp;httpmethod=GET&amp;key1=id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这个api的请求参数是：\n",
    "<img src=\"https://gitee.com/ni1o1/pygeo-tutorial/raw/master/resource/weibo1.png\"  style=\"width:600px\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在api测试工具里面，我们可以找到我们的access_token！那么，把这些参数放到api测试工具里面增加api参数，点击调用参口，可以看到上面的对话框里面出现了我们要的链接\n",
    "<img src=\"https://gitee.com/ni1o1/pygeo-tutorial/raw/master/resource/weibo2.png\"  style=\"width:600px\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "先来抓一条微博，是讲女权的，微博的id是4503823458057845，里面的评论可精彩了值得一抓！[戳这里看内容](https://weibo.com/1887344341/J1BsRxOdf?from=page_1006061887344341_profile&wvr=6&mod=weibotime)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T14:50:23.236680Z",
     "start_time": "2020-05-12T14:50:16.855769Z"
    }
   },
   "outputs": [],
   "source": [
    "#请求URL\n",
    "url = r'https://api.weibo.com/2/comments/show.json'\n",
    "#请求参数：\n",
    "para = r'id=4503823458057845&access_token=xxxxxxxxxxxxxxxxxxxxxxxxx&count=200&page=1'\n",
    "\n",
    "#构建url\n",
    "url = url+'?'+para\n",
    "#抓取\n",
    "request = urllib.request.Request(url)\n",
    "response = urllib.request.urlopen(request)\n",
    "data = response.read().decode('utf-8')\n",
    "#读取json\n",
    "data = json.loads(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#那么数据就已经被我们抓下来了，可以看看长啥样\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T14:50:24.394670Z",
     "start_time": "2020-05-12T14:50:24.278981Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>created_at</th>\n",
       "      <th>id</th>\n",
       "      <th>rootid</th>\n",
       "      <th>rootidstr</th>\n",
       "      <th>floor_number</th>\n",
       "      <th>text</th>\n",
       "      <th>disable_reply</th>\n",
       "      <th>user</th>\n",
       "      <th>mid</th>\n",
       "      <th>idstr</th>\n",
       "      <th>status</th>\n",
       "      <th>reply_comment</th>\n",
       "      <th>reply_original_text</th>\n",
       "      <th>readtimetype</th>\n",
       "      <th>comment_badge</th>\n",
       "      <th>extend_info</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>Tue May 12 22:50:15 +0800 2020</td>\n",
       "      <td>4503892094632524</td>\n",
       "      <td>4503877749088959</td>\n",
       "      <td>4503877749088959</td>\n",
       "      <td>0</td>\n",
       "      <td>回复@青荼蘼:就你这阴阳怪气的评论，普通人看到只会厌恶，这对女权的推广是极为不利的，戾气太重...</td>\n",
       "      <td>0</td>\n",
       "      <td>{'id': 3882791691, 'idstr': '3882791691', 'cla...</td>\n",
       "      <td>4503892094632524</td>\n",
       "      <td>4503892094632524</td>\n",
       "      <td>{'visible': {'type': 0, 'list_id': 0}, 'create...</td>\n",
       "      <td>{'created_at': 'Tue May 12 21:53:15 +0800 2020...</td>\n",
       "      <td>就你这阴阳怪气的评论，普通人看到只会厌恶，这对女权的推广是极为不利的，戾气太重，导致很多对女...</td>\n",
       "      <td>comment</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>Tue May 12 22:50:01 +0800 2020</td>\n",
       "      <td>4503892030895239</td>\n",
       "      <td>4503842027558210</td>\n",
       "      <td>4503842027558210</td>\n",
       "      <td>0</td>\n",
       "      <td>回复@喷火的龙之传人:反婚反育不就好了。女性基本找不到同意让冠姓权的男性，楼里那位女性反婚反...</td>\n",
       "      <td>0</td>\n",
       "      <td>{'id': 7041459439, 'idstr': '7041459439', 'cla...</td>\n",
       "      <td>4503892030895239</td>\n",
       "      <td>4503892030895239</td>\n",
       "      <td>{'visible': {'type': 0, 'list_id': 0}, 'create...</td>\n",
       "      <td>{'created_at': 'Tue May 12 19:31:19 +0800 2020...</td>\n",
       "      <td>反婚反育不就好了。女性基本找不到同意让冠姓权的男性，楼里那位女性反婚反育应该影响不到你吧。。</td>\n",
       "      <td>comment</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>Tue May 12 22:49:14 +0800 2020</td>\n",
       "      <td>4503891834205300</td>\n",
       "      <td>4503825996628106</td>\n",
       "      <td>4503825996628106</td>\n",
       "      <td>0</td>\n",
       "      <td>回复@Digital曦:你不是，那人家也不是骂你。作为一个网民随意发表点言论，引来一群蛆骂，...</td>\n",
       "      <td>0</td>\n",
       "      <td>{'id': 1167831071, 'idstr': '1167831071', 'cla...</td>\n",
       "      <td>4503891834205300</td>\n",
       "      <td>4503891834205300</td>\n",
       "      <td>{'visible': {'type': 0, 'list_id': 0}, 'create...</td>\n",
       "      <td>{'created_at': 'Tue May 12 22:46:13 +0800 2020...</td>\n",
       "      <td>你不是，那人家也不是骂你。作为一个网民随意发表点言论，引来一群蛆骂，那人家当然要骂回去。</td>\n",
       "      <td>comment</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>Tue May 12 22:48:57 +0800 2020</td>\n",
       "      <td>4503891762916223</td>\n",
       "      <td>4503823655872375</td>\n",
       "      <td>4503823655872375</td>\n",
       "      <td>0</td>\n",
       "      <td>回复@鹧鸪孤鶱:可她就是贱驴啊，让孩子随父姓不是驴是什么。</td>\n",
       "      <td>0</td>\n",
       "      <td>{'id': 5506099264, 'idstr': '5506099264', 'cla...</td>\n",
       "      <td>4503891762916223</td>\n",
       "      <td>4503891762916223</td>\n",
       "      <td>{'visible': {'type': 0, 'list_id': 0}, 'create...</td>\n",
       "      <td>{'created_at': 'Tue May 12 18:18:19 +0800 2020...</td>\n",
       "      <td>可她就是贱驴啊，让孩子随父姓不是驴是什么。</td>\n",
       "      <td>comment</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>Tue May 12 22:48:52 +0800 2020</td>\n",
       "      <td>4503891741431696</td>\n",
       "      <td>4503824926864646</td>\n",
       "      <td>4503824926864646</td>\n",
       "      <td>0</td>\n",
       "      <td>回复@l若愚l:你家过日子不是互相伤害，你家是你和你爸俩大鼎占你妈便宜……你能把对立理解成互...</td>\n",
       "      <td>0</td>\n",
       "      <td>{'id': 1549170040, 'idstr': '1549170040', 'cla...</td>\n",
       "      <td>4503891741431696</td>\n",
       "      <td>4503891741431696</td>\n",
       "      <td>{'visible': {'type': 0, 'list_id': 0}, 'create...</td>\n",
       "      <td>{'created_at': 'Tue May 12 22:39:34 +0800 2020...</td>\n",
       "      <td>你家过日子不是互相伤害，你家是你和你爸俩大鼎占你妈便宜……你能把对立理解成互相伤害真是智商低...</td>\n",
       "      <td>comment</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       created_at                id            rootid  \\\n",
       "0  Tue May 12 22:50:15 +0800 2020  4503892094632524  4503877749088959   \n",
       "1  Tue May 12 22:50:01 +0800 2020  4503892030895239  4503842027558210   \n",
       "2  Tue May 12 22:49:14 +0800 2020  4503891834205300  4503825996628106   \n",
       "3  Tue May 12 22:48:57 +0800 2020  4503891762916223  4503823655872375   \n",
       "4  Tue May 12 22:48:52 +0800 2020  4503891741431696  4503824926864646   \n",
       "\n",
       "          rootidstr  floor_number  \\\n",
       "0  4503877749088959             0   \n",
       "1  4503842027558210             0   \n",
       "2  4503825996628106             0   \n",
       "3  4503823655872375             0   \n",
       "4  4503824926864646             0   \n",
       "\n",
       "                                                text  disable_reply  \\\n",
       "0  回复@青荼蘼:就你这阴阳怪气的评论，普通人看到只会厌恶，这对女权的推广是极为不利的，戾气太重...              0   \n",
       "1  回复@喷火的龙之传人:反婚反育不就好了。女性基本找不到同意让冠姓权的男性，楼里那位女性反婚反...              0   \n",
       "2  回复@Digital曦:你不是，那人家也不是骂你。作为一个网民随意发表点言论，引来一群蛆骂，...              0   \n",
       "3                      回复@鹧鸪孤鶱:可她就是贱驴啊，让孩子随父姓不是驴是什么。              0   \n",
       "4  回复@l若愚l:你家过日子不是互相伤害，你家是你和你爸俩大鼎占你妈便宜……你能把对立理解成互...              0   \n",
       "\n",
       "                                                user               mid  \\\n",
       "0  {'id': 3882791691, 'idstr': '3882791691', 'cla...  4503892094632524   \n",
       "1  {'id': 7041459439, 'idstr': '7041459439', 'cla...  4503892030895239   \n",
       "2  {'id': 1167831071, 'idstr': '1167831071', 'cla...  4503891834205300   \n",
       "3  {'id': 5506099264, 'idstr': '5506099264', 'cla...  4503891762916223   \n",
       "4  {'id': 1549170040, 'idstr': '1549170040', 'cla...  4503891741431696   \n",
       "\n",
       "              idstr                                             status  \\\n",
       "0  4503892094632524  {'visible': {'type': 0, 'list_id': 0}, 'create...   \n",
       "1  4503892030895239  {'visible': {'type': 0, 'list_id': 0}, 'create...   \n",
       "2  4503891834205300  {'visible': {'type': 0, 'list_id': 0}, 'create...   \n",
       "3  4503891762916223  {'visible': {'type': 0, 'list_id': 0}, 'create...   \n",
       "4  4503891741431696  {'visible': {'type': 0, 'list_id': 0}, 'create...   \n",
       "\n",
       "                                       reply_comment  \\\n",
       "0  {'created_at': 'Tue May 12 21:53:15 +0800 2020...   \n",
       "1  {'created_at': 'Tue May 12 19:31:19 +0800 2020...   \n",
       "2  {'created_at': 'Tue May 12 22:46:13 +0800 2020...   \n",
       "3  {'created_at': 'Tue May 12 18:18:19 +0800 2020...   \n",
       "4  {'created_at': 'Tue May 12 22:39:34 +0800 2020...   \n",
       "\n",
       "                                 reply_original_text readtimetype  \\\n",
       "0  就你这阴阳怪气的评论，普通人看到只会厌恶，这对女权的推广是极为不利的，戾气太重，导致很多对女...      comment   \n",
       "1     反婚反育不就好了。女性基本找不到同意让冠姓权的男性，楼里那位女性反婚反育应该影响不到你吧。。      comment   \n",
       "2       你不是，那人家也不是骂你。作为一个网民随意发表点言论，引来一群蛆骂，那人家当然要骂回去。      comment   \n",
       "3                              可她就是贱驴啊，让孩子随父姓不是驴是什么。      comment   \n",
       "4  你家过日子不是互相伤害，你家是你和你爸俩大鼎占你妈便宜……你能把对立理解成互相伤害真是智商低...      comment   \n",
       "\n",
       "  comment_badge extend_info  \n",
       "0           NaN         NaN  \n",
       "1           NaN         NaN  \n",
       "2           NaN         NaN  \n",
       "3           NaN         NaN  \n",
       "4           NaN         NaN  "
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#这个json文件里面的comments可以直接转换为pd的dataframe\n",
    "data = pd.DataFrame(data['comments'])\n",
    "data.head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T14:33:50.898210Z",
     "start_time": "2020-05-12T14:33:50.893185Z"
    }
   },
   "source": [
    "不过，这里咱把全部评论抓下来，怎么抓呢？  \n",
    "可以看到，上面请求参数里面有个page参数  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "没错！很容易！就是page从1到正无穷，直到抓到评论数量为0就停"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T15:02:33.633169Z",
     "start_time": "2020-05-12T15:02:07.846017Z"
    }
   },
   "outputs": [],
   "source": [
    "#初始化datalen参数记录每次抓到的评论数\n",
    "datalen = 1\n",
    "#从第一页开始抓\n",
    "page = 1\n",
    "#初始化url\n",
    "url = r'https://api.weibo.com/2/comments/show.json'\n",
    "#初始化请求参数\n",
    "para = r'id=4503823458057845&access_token=xxxxxxxxxxxxxxxxxxxxxxxxx&count=200&page=1'\n",
    "#每次抓的数据存储到一个list中\n",
    "result = []\n",
    "\n",
    "while datalen>0:\n",
    "    #请求参数：\n",
    "    para1 = para[:-1]+str(page)\n",
    "    #构建url\n",
    "    url1 = url+'?'+para1\n",
    "    print(url1)\n",
    "    \n",
    "    #抓取\n",
    "    request = urllib.request.Request(url1)\n",
    "    response = urllib.request.urlopen(request)\n",
    "    data = response.read().decode('utf-8')\n",
    "    \n",
    "    #读取json\n",
    "    data = json.loads(data)\n",
    "\n",
    "    #这个json文件里面的comments可以直接转换为pd的dataframe\n",
    "    data = pd.DataFrame(data['comments'])\n",
    "    \n",
    "    #看看抓到多少条数据\n",
    "    datalen = len(data)\n",
    "    \n",
    "    #如果有数据，就放进result里\n",
    "    if datalen>0:\n",
    "        result.append(data)\n",
    "    #抓下一页\n",
    "    page+=1\n",
    "\n",
    "#合并为一个DataFrame\n",
    "result = pd.concat(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T15:02:53.436513Z",
     "start_time": "2020-05-12T15:02:53.432519Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "829"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "好的我一共抓到了829条微博评论"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T15:03:25.173263Z",
     "start_time": "2020-05-12T15:03:25.119409Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>comment_badge</th>\n",
       "      <th>created_at</th>\n",
       "      <th>disable_reply</th>\n",
       "      <th>extend_info</th>\n",
       "      <th>floor_number</th>\n",
       "      <th>id</th>\n",
       "      <th>idstr</th>\n",
       "      <th>mid</th>\n",
       "      <th>readtimetype</th>\n",
       "      <th>reply_comment</th>\n",
       "      <th>reply_original_text</th>\n",
       "      <th>rootid</th>\n",
       "      <th>rootidstr</th>\n",
       "      <th>status</th>\n",
       "      <th>text</th>\n",
       "      <th>user</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>[{'pic_url': 'https://h5.sinaimg.cn/upload/100...</td>\n",
       "      <td>Tue May 12 23:01:58 +0800 2020</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>347</td>\n",
       "      <td>4503895039318273</td>\n",
       "      <td>4503895039318273</td>\n",
       "      <td>4503895039318273</td>\n",
       "      <td>comment</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4503895039318273</td>\n",
       "      <td>4503895039318273</td>\n",
       "      <td>{'visible': {'type': 0, 'list_id': 0}, 'create...</td>\n",
       "      <td>呼吁大家孩子出生后称呼18位身份证号，直到孩子成年后让ta决定自己的姓名姓氏[爱你]~关注孩...</td>\n",
       "      <td>{'id': 1496160994, 'idstr': '1496160994', 'cla...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Tue May 12 23:01:31 +0800 2020</td>\n",
       "      <td>0</td>\n",
       "      <td>{'danmaku_time': '3638', 'danmaku_from': '0'}</td>\n",
       "      <td>346</td>\n",
       "      <td>4503894929895229</td>\n",
       "      <td>4503894929895229</td>\n",
       "      <td>4503894929895229</td>\n",
       "      <td>comment</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4503894929895229</td>\n",
       "      <td>4503894929895229</td>\n",
       "      <td>{'visible': {'type': 0, 'list_id': 0}, 'create...</td>\n",
       "      <td>咦？平时有男人嘴炮中国女生是母狗、是eg、是性资源的时候，怎么没见这么多男的义愤填膺？现在倒...</td>\n",
       "      <td>{'id': 5589656207, 'idstr': '5589656207', 'cla...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Tue May 12 23:01:01 +0800 2020</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>4503894799684876</td>\n",
       "      <td>4503894799684876</td>\n",
       "      <td>4503894799684876</td>\n",
       "      <td>comment</td>\n",
       "      <td>{'created_at': 'Tue May 12 20:28:14 +0800 2020...</td>\n",
       "      <td>那没事了[允悲][允悲]可能是我没见到吧，我的</td>\n",
       "      <td>4503827548019096</td>\n",
       "      <td>4503827548019096</td>\n",
       "      <td>{'visible': {'type': 0, 'list_id': 0}, 'create...</td>\n",
       "      <td>回复@一块废柄:那没事了[允悲][允悲]可能是我没见到吧，我的</td>\n",
       "      <td>{'id': 5690080210, 'idstr': '5690080210', 'cla...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Tue May 12 22:57:36 +0800 2020</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>4503893939440479</td>\n",
       "      <td>4503893939440479</td>\n",
       "      <td>4503893939440479</td>\n",
       "      <td>comment</td>\n",
       "      <td>{'created_at': 'Tue May 12 22:50:53 +0800 2020...</td>\n",
       "      <td>谁强迫她了？恩和开始说的也只是陈述句“孩子随父姓”，现在整的我以为有拿枪逼着她给娃改姓呢。不...</td>\n",
       "      <td>4503877749088959</td>\n",
       "      <td>4503877749088959</td>\n",
       "      <td>{'visible': {'type': 0, 'list_id': 0}, 'create...</td>\n",
       "      <td>回复@我家狗子叫小猫:谁强迫她了？恩和开始说的也只是陈述句“孩子随父姓”，现在整的我以为有拿...</td>\n",
       "      <td>{'id': 2274183742, 'idstr': '2274183742', 'cla...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Tue May 12 22:57:26 +0800 2020</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>4503893901477855</td>\n",
       "      <td>4503893901477855</td>\n",
       "      <td>4503893901477855</td>\n",
       "      <td>comment</td>\n",
       "      <td>{'created_at': 'Tue May 12 19:31:19 +0800 2020...</td>\n",
       "      <td>张小美那青春又美丽的脸被我颜射了？我自己也彻底懵逼了。而接下来我要面对的是张小美的勃然大怒—...</td>\n",
       "      <td>4503842027558210</td>\n",
       "      <td>4503842027558210</td>\n",
       "      <td>{'visible': {'type': 0, 'list_id': 0}, 'create...</td>\n",
       "      <td>回复@喷火的龙之传人:张小美那青春又美丽的脸被我颜射了？我自己也彻底懵逼了。而接下来我要面对...</td>\n",
       "      <td>{'id': 1711692274, 'idstr': '1711692274', 'cla...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                       comment_badge  \\\n",
       "0  [{'pic_url': 'https://h5.sinaimg.cn/upload/100...   \n",
       "1                                                NaN   \n",
       "2                                                NaN   \n",
       "3                                                NaN   \n",
       "4                                                NaN   \n",
       "\n",
       "                       created_at  disable_reply  \\\n",
       "0  Tue May 12 23:01:58 +0800 2020              0   \n",
       "1  Tue May 12 23:01:31 +0800 2020              0   \n",
       "2  Tue May 12 23:01:01 +0800 2020              0   \n",
       "3  Tue May 12 22:57:36 +0800 2020              0   \n",
       "4  Tue May 12 22:57:26 +0800 2020              0   \n",
       "\n",
       "                                     extend_info  floor_number  \\\n",
       "0                                            NaN           347   \n",
       "1  {'danmaku_time': '3638', 'danmaku_from': '0'}           346   \n",
       "2                                            NaN             0   \n",
       "3                                            NaN             0   \n",
       "4                                            NaN             0   \n",
       "\n",
       "                 id             idstr               mid readtimetype  \\\n",
       "0  4503895039318273  4503895039318273  4503895039318273      comment   \n",
       "1  4503894929895229  4503894929895229  4503894929895229      comment   \n",
       "2  4503894799684876  4503894799684876  4503894799684876      comment   \n",
       "3  4503893939440479  4503893939440479  4503893939440479      comment   \n",
       "4  4503893901477855  4503893901477855  4503893901477855      comment   \n",
       "\n",
       "                                       reply_comment  \\\n",
       "0                                                NaN   \n",
       "1                                                NaN   \n",
       "2  {'created_at': 'Tue May 12 20:28:14 +0800 2020...   \n",
       "3  {'created_at': 'Tue May 12 22:50:53 +0800 2020...   \n",
       "4  {'created_at': 'Tue May 12 19:31:19 +0800 2020...   \n",
       "\n",
       "                                 reply_original_text            rootid  \\\n",
       "0                                                NaN  4503895039318273   \n",
       "1                                                NaN  4503894929895229   \n",
       "2                            那没事了[允悲][允悲]可能是我没见到吧，我的  4503827548019096   \n",
       "3  谁强迫她了？恩和开始说的也只是陈述句“孩子随父姓”，现在整的我以为有拿枪逼着她给娃改姓呢。不...  4503877749088959   \n",
       "4  张小美那青春又美丽的脸被我颜射了？我自己也彻底懵逼了。而接下来我要面对的是张小美的勃然大怒—...  4503842027558210   \n",
       "\n",
       "          rootidstr                                             status  \\\n",
       "0  4503895039318273  {'visible': {'type': 0, 'list_id': 0}, 'create...   \n",
       "1  4503894929895229  {'visible': {'type': 0, 'list_id': 0}, 'create...   \n",
       "2  4503827548019096  {'visible': {'type': 0, 'list_id': 0}, 'create...   \n",
       "3  4503877749088959  {'visible': {'type': 0, 'list_id': 0}, 'create...   \n",
       "4  4503842027558210  {'visible': {'type': 0, 'list_id': 0}, 'create...   \n",
       "\n",
       "                                                text  \\\n",
       "0  呼吁大家孩子出生后称呼18位身份证号，直到孩子成年后让ta决定自己的姓名姓氏[爱你]~关注孩...   \n",
       "1  咦？平时有男人嘴炮中国女生是母狗、是eg、是性资源的时候，怎么没见这么多男的义愤填膺？现在倒...   \n",
       "2                    回复@一块废柄:那没事了[允悲][允悲]可能是我没见到吧，我的   \n",
       "3  回复@我家狗子叫小猫:谁强迫她了？恩和开始说的也只是陈述句“孩子随父姓”，现在整的我以为有拿...   \n",
       "4  回复@喷火的龙之传人:张小美那青春又美丽的脸被我颜射了？我自己也彻底懵逼了。而接下来我要面对...   \n",
       "\n",
       "                                                user  \n",
       "0  {'id': 1496160994, 'idstr': '1496160994', 'cla...  \n",
       "1  {'id': 5589656207, 'idstr': '5589656207', 'cla...  \n",
       "2  {'id': 5690080210, 'idstr': '5690080210', 'cla...  \n",
       "3  {'id': 2274183742, 'idstr': '2274183742', 'cla...  \n",
       "4  {'id': 1711692274, 'idstr': '1711692274', 'cla...  "
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下一步我们来看看是谁在评论回复谁  \n",
    "首先获取每条评论是谁发的，用户的的信息存储在user列中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T15:18:02.940357Z",
     "start_time": "2020-05-12T15:18:02.905416Z"
    }
   },
   "outputs": [],
   "source": [
    "#发评论的人\n",
    "result['username'] = pd.DataFrame(list(result['user']))['name']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T15:18:04.762666Z",
     "start_time": "2020-05-12T15:18:04.738708Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>created_at</th>\n",
       "      <th>id</th>\n",
       "      <th>rootid</th>\n",
       "      <th>rootidstr</th>\n",
       "      <th>floor_number</th>\n",
       "      <th>text</th>\n",
       "      <th>disable_reply</th>\n",
       "      <th>user</th>\n",
       "      <th>mid</th>\n",
       "      <th>idstr</th>\n",
       "      <th>extend_info</th>\n",
       "      <th>comment_badge</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>Tue May 12 20:28:14 +0800 2020</td>\n",
       "      <td>4503856354178157</td>\n",
       "      <td>4503827548019096</td>\n",
       "      <td>4503827548019096</td>\n",
       "      <td>0</td>\n",
       "      <td>回复@ZWWzwc:我身边搞女权的都是985211清华北大伦敦大学啊[疑问]有什么问题吗</td>\n",
       "      <td>0</td>\n",
       "      <td>{'id': 7274821563, 'idstr': '7274821563', 'cla...</td>\n",
       "      <td>4503856354178157</td>\n",
       "      <td>4503856354178157</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>Tue May 12 22:50:53 +0800 2020</td>\n",
       "      <td>4503892254244050</td>\n",
       "      <td>4503877749088959</td>\n",
       "      <td>4503877749088959</td>\n",
       "      <td>0</td>\n",
       "      <td>回复@青荼蘼:你觉得冠姓权再重要，当事人觉得不重要。那你们还要强迫别人啊？一个人作出任何选择...</td>\n",
       "      <td>0</td>\n",
       "      <td>{'id': 5638974069, 'idstr': '5638974069', 'cla...</td>\n",
       "      <td>4503892254244050</td>\n",
       "      <td>4503892254244050</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>Tue May 12 19:31:19 +0800 2020</td>\n",
       "      <td>4503842027558210</td>\n",
       "      <td>4503842027558210</td>\n",
       "      <td>4503842027558210</td>\n",
       "      <td>235</td>\n",
       "      <td>我笑了。男的可是宁愿杀妻也不要孩子随母姓，男的宁愿离婚不要孩子随母姓……呢。姓氏不重要哈[嘻...</td>\n",
       "      <td>0</td>\n",
       "      <td>{'id': 6927664310, 'idstr': '6927664310', 'cla...</td>\n",
       "      <td>4503842027558210</td>\n",
       "      <td>4503842027558210</td>\n",
       "      <td>{'danmaku_time': '52339', 'danmaku_from': '0'}</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>Tue May 12 22:50:15 +0800 2020</td>\n",
       "      <td>4503892094632524</td>\n",
       "      <td>4503877749088959</td>\n",
       "      <td>4503877749088959</td>\n",
       "      <td>0</td>\n",
       "      <td>回复@青荼蘼:就你这阴阳怪气的评论，普通人看到只会厌恶，这对女权的推广是极为不利的，戾气太重...</td>\n",
       "      <td>0</td>\n",
       "      <td>{'id': 3882791691, 'idstr': '3882791691', 'cla...</td>\n",
       "      <td>4503892094632524</td>\n",
       "      <td>4503892094632524</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>Tue May 12 21:12:49 +0800 2020</td>\n",
       "      <td>4503867574885068</td>\n",
       "      <td>4503825493524912</td>\n",
       "      <td>4503825493524912</td>\n",
       "      <td>0</td>\n",
       "      <td>回复@我给老板画大饼:欧美婚都不怎么结了，法国非婚生子2007年就超过百分之六十了[摊手]</td>\n",
       "      <td>0</td>\n",
       "      <td>{'id': 2146690095, 'idstr': '2146690095', 'cla...</td>\n",
       "      <td>4503867574885068</td>\n",
       "      <td>4503867574885068</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       created_at                id            rootid  \\\n",
       "0  Tue May 12 20:28:14 +0800 2020  4503856354178157  4503827548019096   \n",
       "1  Tue May 12 22:50:53 +0800 2020  4503892254244050  4503877749088959   \n",
       "2  Tue May 12 19:31:19 +0800 2020  4503842027558210  4503842027558210   \n",
       "3  Tue May 12 22:50:15 +0800 2020  4503892094632524  4503877749088959   \n",
       "4  Tue May 12 21:12:49 +0800 2020  4503867574885068  4503825493524912   \n",
       "\n",
       "          rootidstr  floor_number  \\\n",
       "0  4503827548019096             0   \n",
       "1  4503877749088959             0   \n",
       "2  4503842027558210           235   \n",
       "3  4503877749088959             0   \n",
       "4  4503825493524912             0   \n",
       "\n",
       "                                                text  disable_reply  \\\n",
       "0       回复@ZWWzwc:我身边搞女权的都是985211清华北大伦敦大学啊[疑问]有什么问题吗              0   \n",
       "1  回复@青荼蘼:你觉得冠姓权再重要，当事人觉得不重要。那你们还要强迫别人啊？一个人作出任何选择...              0   \n",
       "2  我笑了。男的可是宁愿杀妻也不要孩子随母姓，男的宁愿离婚不要孩子随母姓……呢。姓氏不重要哈[嘻...              0   \n",
       "3  回复@青荼蘼:就你这阴阳怪气的评论，普通人看到只会厌恶，这对女权的推广是极为不利的，戾气太重...              0   \n",
       "4      回复@我给老板画大饼:欧美婚都不怎么结了，法国非婚生子2007年就超过百分之六十了[摊手]              0   \n",
       "\n",
       "                                                user               mid  \\\n",
       "0  {'id': 7274821563, 'idstr': '7274821563', 'cla...  4503856354178157   \n",
       "1  {'id': 5638974069, 'idstr': '5638974069', 'cla...  4503892254244050   \n",
       "2  {'id': 6927664310, 'idstr': '6927664310', 'cla...  4503842027558210   \n",
       "3  {'id': 3882791691, 'idstr': '3882791691', 'cla...  4503892094632524   \n",
       "4  {'id': 2146690095, 'idstr': '2146690095', 'cla...  4503867574885068   \n",
       "\n",
       "              idstr                                     extend_info  \\\n",
       "0  4503856354178157                                             NaN   \n",
       "1  4503892254244050                                             NaN   \n",
       "2  4503842027558210  {'danmaku_time': '52339', 'danmaku_from': '0'}   \n",
       "3  4503892094632524                                             NaN   \n",
       "4  4503867574885068                                             NaN   \n",
       "\n",
       "  comment_badge  \n",
       "0           NaN  \n",
       "1           NaN  \n",
       "2           NaN  \n",
       "3           NaN  \n",
       "4           NaN  "
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#有些评论是回复别人的，回复的信息存储在reply_comment列中。\n",
    "#有些有有些没，我们把这些有回复的提取出来\n",
    "pd.DataFrame(list(result[-result['reply_comment'].isnull()]['reply_comment'])).head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T15:19:39.926842Z",
     "start_time": "2020-05-12T15:19:39.885914Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0            一块废柄\n",
       "1         我家狗子叫小猫\n",
       "2         喷火的龙之传人\n",
       "3         脾气暴躁的乐乐\n",
       "4      总有一天我也会有猫的\n",
       "          ...    \n",
       "532        爱足球的小辛\n",
       "533          鹧鸪孤鶱\n",
       "534       红尘陌上097\n",
       "535          鹧鸪孤鶱\n",
       "536        爱足球的小辛\n",
       "Name: name, Length: 537, dtype: object"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#有些有有些没，我们把这些有回复的提取出来，看看他们回复谁的评论\n",
    "pd.DataFrame(list(pd.DataFrame(list(result[-result['reply_comment'].isnull()]['reply_comment']))['user']))['name']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T15:20:27.893446Z",
     "start_time": "2020-05-12T15:20:27.866475Z"
    }
   },
   "outputs": [],
   "source": [
    "#赋值回去\n",
    "result.loc[-result['reply_comment'].isnull(),'replyname'] = list(pd.DataFrame(list(pd.DataFrame(list(result[-result['reply_comment'].isnull()]['reply_comment']))['user']))['name'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T15:20:34.485305Z",
     "start_time": "2020-05-12T15:20:34.375619Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>comment_badge</th>\n",
       "      <th>created_at</th>\n",
       "      <th>disable_reply</th>\n",
       "      <th>extend_info</th>\n",
       "      <th>floor_number</th>\n",
       "      <th>id</th>\n",
       "      <th>idstr</th>\n",
       "      <th>mid</th>\n",
       "      <th>readtimetype</th>\n",
       "      <th>reply_comment</th>\n",
       "      <th>reply_original_text</th>\n",
       "      <th>rootid</th>\n",
       "      <th>rootidstr</th>\n",
       "      <th>status</th>\n",
       "      <th>text</th>\n",
       "      <th>user</th>\n",
       "      <th>username</th>\n",
       "      <th>replyname</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Tue May 12 23:01:01 +0800 2020</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>4503894799684876</td>\n",
       "      <td>4503894799684876</td>\n",
       "      <td>4503894799684876</td>\n",
       "      <td>comment</td>\n",
       "      <td>{'created_at': 'Tue May 12 20:28:14 +0800 2020...</td>\n",
       "      <td>那没事了[允悲][允悲]可能是我没见到吧，我的</td>\n",
       "      <td>4503827548019096</td>\n",
       "      <td>4503827548019096</td>\n",
       "      <td>{'visible': {'type': 0, 'list_id': 0}, 'create...</td>\n",
       "      <td>回复@一块废柄:那没事了[允悲][允悲]可能是我没见到吧，我的</td>\n",
       "      <td>{'id': 5690080210, 'idstr': '5690080210', 'cla...</td>\n",
       "      <td>ZWWzwc</td>\n",
       "      <td>一块废柄</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Tue May 12 22:57:36 +0800 2020</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>4503893939440479</td>\n",
       "      <td>4503893939440479</td>\n",
       "      <td>4503893939440479</td>\n",
       "      <td>comment</td>\n",
       "      <td>{'created_at': 'Tue May 12 22:50:53 +0800 2020...</td>\n",
       "      <td>谁强迫她了？恩和开始说的也只是陈述句“孩子随父姓”，现在整的我以为有拿枪逼着她给娃改姓呢。不...</td>\n",
       "      <td>4503877749088959</td>\n",
       "      <td>4503877749088959</td>\n",
       "      <td>{'visible': {'type': 0, 'list_id': 0}, 'create...</td>\n",
       "      <td>回复@我家狗子叫小猫:谁强迫她了？恩和开始说的也只是陈述句“孩子随父姓”，现在整的我以为有拿...</td>\n",
       "      <td>{'id': 2274183742, 'idstr': '2274183742', 'cla...</td>\n",
       "      <td>青荼蘼</td>\n",
       "      <td>我家狗子叫小猫</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Tue May 12 22:57:26 +0800 2020</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>4503893901477855</td>\n",
       "      <td>4503893901477855</td>\n",
       "      <td>4503893901477855</td>\n",
       "      <td>comment</td>\n",
       "      <td>{'created_at': 'Tue May 12 19:31:19 +0800 2020...</td>\n",
       "      <td>张小美那青春又美丽的脸被我颜射了？我自己也彻底懵逼了。而接下来我要面对的是张小美的勃然大怒—...</td>\n",
       "      <td>4503842027558210</td>\n",
       "      <td>4503842027558210</td>\n",
       "      <td>{'visible': {'type': 0, 'list_id': 0}, 'create...</td>\n",
       "      <td>回复@喷火的龙之传人:张小美那青春又美丽的脸被我颜射了？我自己也彻底懵逼了。而接下来我要面对...</td>\n",
       "      <td>{'id': 1711692274, 'idstr': '1711692274', 'cla...</td>\n",
       "      <td>曲别流殇_898</td>\n",
       "      <td>喷火的龙之传人</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Tue May 12 22:54:52 +0800 2020</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>4503893255845187</td>\n",
       "      <td>4503893255845187</td>\n",
       "      <td>4503893255845187</td>\n",
       "      <td>comment</td>\n",
       "      <td>{'created_at': 'Tue May 12 22:50:15 +0800 2020...</td>\n",
       "      <td>我又不结婚，冠姓权本来也不关我事儿？女权本来就是喜欢就来，不爽就走，又没组织没纪律。咋，还发...</td>\n",
       "      <td>4503877749088959</td>\n",
       "      <td>4503877749088959</td>\n",
       "      <td>{'visible': {'type': 0, 'list_id': 0}, 'create...</td>\n",
       "      <td>回复@脾气暴躁的乐乐:我又不结婚，冠姓权本来也不关我事儿？女权本来就是喜欢就来，不爽就走，又...</td>\n",
       "      <td>{'id': 2274183742, 'idstr': '2274183742', 'cla...</td>\n",
       "      <td>青荼蘼</td>\n",
       "      <td>脾气暴躁的乐乐</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>6</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Tue May 12 22:54:01 +0800 2020</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>4503893037568336</td>\n",
       "      <td>4503893037568336</td>\n",
       "      <td>4503893037568336</td>\n",
       "      <td>comment</td>\n",
       "      <td>{'created_at': 'Tue May 12 21:12:49 +0800 2020...</td>\n",
       "      <td>可惜他们看不到这些</td>\n",
       "      <td>4503825493524912</td>\n",
       "      <td>4503825493524912</td>\n",
       "      <td>{'visible': {'type': 0, 'list_id': 0}, 'create...</td>\n",
       "      <td>回复@总有一天我也会有猫的:可惜他们看不到这些</td>\n",
       "      <td>{'id': 7183137389, 'idstr': '7183137389', 'cla...</td>\n",
       "      <td>孝道将人奴化</td>\n",
       "      <td>总有一天我也会有猫的</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>22</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Tue May 12 18:22:49 +0800 2020</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>4503824792754234</td>\n",
       "      <td>4503824792754234</td>\n",
       "      <td>4503824792754234</td>\n",
       "      <td>comment</td>\n",
       "      <td>{'created_at': 'Tue May 12 18:18:47 +0800 2020...</td>\n",
       "      <td>为什么打在兄弟上，没有姐妹， 为什么是公屏，不是母屏，恶臭蝻权就是你吧[doge][doge...</td>\n",
       "      <td>4503823777926509</td>\n",
       "      <td>4503823777926509</td>\n",
       "      <td>{'visible': {'type': 0, 'list_id': 0}, 'create...</td>\n",
       "      <td>回复@爱足球的小辛:为什么打在兄弟上，没有姐妹， 为什么是公屏，不是母屏，恶臭蝻权就是你吧[...</td>\n",
       "      <td>{'id': 2117684405, 'idstr': '2117684405', 'cla...</td>\n",
       "      <td>Digital曦</td>\n",
       "      <td>爱足球的小辛</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>23</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Tue May 12 18:22:48 +0800 2020</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>4503824788198275</td>\n",
       "      <td>4503824788198275</td>\n",
       "      <td>4503824788198275</td>\n",
       "      <td>comment</td>\n",
       "      <td>{'created_at': 'Tue May 12 18:18:19 +0800 2020...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4503823655872375</td>\n",
       "      <td>4503823655872375</td>\n",
       "      <td>{'visible': {'type': 0, 'list_id': 0}, 'create...</td>\n",
       "      <td>微博上的\"女权主义\": 你这个女人怎么跟我的观念不一样，我要为女性争取权利，我要先把你这头母...</td>\n",
       "      <td>{'id': 7189228727, 'idstr': '7189228727', 'cla...</td>\n",
       "      <td>炒饭大师199402</td>\n",
       "      <td>鹧鸪孤鶱</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>35</td>\n",
       "      <td>[{'pic_url': 'https://h5.sinaimg.cn/upload/100...</td>\n",
       "      <td>Tue May 12 18:20:53 +0800 2020</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>4503824305431543</td>\n",
       "      <td>4503824305431543</td>\n",
       "      <td>4503824305431543</td>\n",
       "      <td>comment</td>\n",
       "      <td>{'created_at': 'Tue May 12 18:19:11 +0800 2020...</td>\n",
       "      <td>卑鄙是卑鄙者的通行证</td>\n",
       "      <td>4503823877728490</td>\n",
       "      <td>4503823877728490</td>\n",
       "      <td>{'visible': {'type': 0, 'list_id': 0}, 'create...</td>\n",
       "      <td>回复@红尘陌上097:卑鄙是卑鄙者的通行证</td>\n",
       "      <td>{'id': 2426432731, 'idstr': '2426432731', 'cla...</td>\n",
       "      <td>Artemis婷婷_貂爷家皮草</td>\n",
       "      <td>红尘陌上097</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>37</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Tue May 12 18:20:49 +0800 2020</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>4503824289659100</td>\n",
       "      <td>4503824289659100</td>\n",
       "      <td>4503824289659100</td>\n",
       "      <td>comment</td>\n",
       "      <td>{'created_at': 'Tue May 12 18:18:19 +0800 2020...</td>\n",
       "      <td>对啊，现在网络喷子质量堪忧啊  哈哈哈哈哈哈 搞不懂人家生孩子随谁姓关他们啥事。还是吃的太饱...</td>\n",
       "      <td>4503823655872375</td>\n",
       "      <td>4503823655872375</td>\n",
       "      <td>{'visible': {'type': 0, 'list_id': 0}, 'create...</td>\n",
       "      <td>回复@鹧鸪孤鶱:对啊，现在网络喷子质量堪忧啊  哈哈哈哈哈哈 搞不懂人家生孩子随谁姓关他们啥...</td>\n",
       "      <td>{'id': 6130736773, 'idstr': '6130736773', 'cla...</td>\n",
       "      <td>肉肉肉肉啊08256</td>\n",
       "      <td>鹧鸪孤鶱</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>40</td>\n",
       "      <td>[{'pic_url': 'https://h5.sinaimg.cn/upload/100...</td>\n",
       "      <td>Tue May 12 18:20:37 +0800 2020</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>4503824234801055</td>\n",
       "      <td>4503824234801055</td>\n",
       "      <td>4503824234801055</td>\n",
       "      <td>comment</td>\n",
       "      <td>{'created_at': 'Tue May 12 18:18:47 +0800 2020...</td>\n",
       "      <td>保护们把乱拳打在兄弟身上，别被⭕️法家抢了人头[笑哈哈][笑哈哈]</td>\n",
       "      <td>4503823777926509</td>\n",
       "      <td>4503823777926509</td>\n",
       "      <td>{'visible': {'type': 0, 'list_id': 0}, 'create...</td>\n",
       "      <td>回复@爱足球的小辛:保护们把乱拳打在兄弟身上，别被⭕️法家抢了人头[笑哈哈][笑哈哈]</td>\n",
       "      <td>{'id': 2179439900, 'idstr': '2179439900', 'cla...</td>\n",
       "      <td>炒饭大师199402</td>\n",
       "      <td>爱足球的小辛</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>537 rows × 18 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                        comment_badge  \\\n",
       "2                                                 NaN   \n",
       "3                                                 NaN   \n",
       "4                                                 NaN   \n",
       "5                                                 NaN   \n",
       "6                                                 NaN   \n",
       "..                                                ...   \n",
       "22                                                NaN   \n",
       "23                                                NaN   \n",
       "35  [{'pic_url': 'https://h5.sinaimg.cn/upload/100...   \n",
       "37                                                NaN   \n",
       "40  [{'pic_url': 'https://h5.sinaimg.cn/upload/100...   \n",
       "\n",
       "                        created_at  disable_reply extend_info  floor_number  \\\n",
       "2   Tue May 12 23:01:01 +0800 2020              0         NaN             0   \n",
       "3   Tue May 12 22:57:36 +0800 2020              0         NaN             0   \n",
       "4   Tue May 12 22:57:26 +0800 2020              0         NaN             0   \n",
       "5   Tue May 12 22:54:52 +0800 2020              0         NaN             0   \n",
       "6   Tue May 12 22:54:01 +0800 2020              0         NaN             0   \n",
       "..                             ...            ...         ...           ...   \n",
       "22  Tue May 12 18:22:49 +0800 2020              0         NaN             0   \n",
       "23  Tue May 12 18:22:48 +0800 2020              0         NaN             0   \n",
       "35  Tue May 12 18:20:53 +0800 2020              0         NaN             0   \n",
       "37  Tue May 12 18:20:49 +0800 2020              0         NaN             0   \n",
       "40  Tue May 12 18:20:37 +0800 2020              0         NaN             0   \n",
       "\n",
       "                  id             idstr               mid readtimetype  \\\n",
       "2   4503894799684876  4503894799684876  4503894799684876      comment   \n",
       "3   4503893939440479  4503893939440479  4503893939440479      comment   \n",
       "4   4503893901477855  4503893901477855  4503893901477855      comment   \n",
       "5   4503893255845187  4503893255845187  4503893255845187      comment   \n",
       "6   4503893037568336  4503893037568336  4503893037568336      comment   \n",
       "..               ...               ...               ...          ...   \n",
       "22  4503824792754234  4503824792754234  4503824792754234      comment   \n",
       "23  4503824788198275  4503824788198275  4503824788198275      comment   \n",
       "35  4503824305431543  4503824305431543  4503824305431543      comment   \n",
       "37  4503824289659100  4503824289659100  4503824289659100      comment   \n",
       "40  4503824234801055  4503824234801055  4503824234801055      comment   \n",
       "\n",
       "                                        reply_comment  \\\n",
       "2   {'created_at': 'Tue May 12 20:28:14 +0800 2020...   \n",
       "3   {'created_at': 'Tue May 12 22:50:53 +0800 2020...   \n",
       "4   {'created_at': 'Tue May 12 19:31:19 +0800 2020...   \n",
       "5   {'created_at': 'Tue May 12 22:50:15 +0800 2020...   \n",
       "6   {'created_at': 'Tue May 12 21:12:49 +0800 2020...   \n",
       "..                                                ...   \n",
       "22  {'created_at': 'Tue May 12 18:18:47 +0800 2020...   \n",
       "23  {'created_at': 'Tue May 12 18:18:19 +0800 2020...   \n",
       "35  {'created_at': 'Tue May 12 18:19:11 +0800 2020...   \n",
       "37  {'created_at': 'Tue May 12 18:18:19 +0800 2020...   \n",
       "40  {'created_at': 'Tue May 12 18:18:47 +0800 2020...   \n",
       "\n",
       "                                  reply_original_text            rootid  \\\n",
       "2                             那没事了[允悲][允悲]可能是我没见到吧，我的  4503827548019096   \n",
       "3   谁强迫她了？恩和开始说的也只是陈述句“孩子随父姓”，现在整的我以为有拿枪逼着她给娃改姓呢。不...  4503877749088959   \n",
       "4   张小美那青春又美丽的脸被我颜射了？我自己也彻底懵逼了。而接下来我要面对的是张小美的勃然大怒—...  4503842027558210   \n",
       "5   我又不结婚，冠姓权本来也不关我事儿？女权本来就是喜欢就来，不爽就走，又没组织没纪律。咋，还发...  4503877749088959   \n",
       "6                                           可惜他们看不到这些  4503825493524912   \n",
       "..                                                ...               ...   \n",
       "22  为什么打在兄弟上，没有姐妹， 为什么是公屏，不是母屏，恶臭蝻权就是你吧[doge][doge...  4503823777926509   \n",
       "23                                                NaN  4503823655872375   \n",
       "35                                         卑鄙是卑鄙者的通行证  4503823877728490   \n",
       "37  对啊，现在网络喷子质量堪忧啊  哈哈哈哈哈哈 搞不懂人家生孩子随谁姓关他们啥事。还是吃的太饱...  4503823655872375   \n",
       "40                  保护们把乱拳打在兄弟身上，别被⭕️法家抢了人头[笑哈哈][笑哈哈]  4503823777926509   \n",
       "\n",
       "           rootidstr                                             status  \\\n",
       "2   4503827548019096  {'visible': {'type': 0, 'list_id': 0}, 'create...   \n",
       "3   4503877749088959  {'visible': {'type': 0, 'list_id': 0}, 'create...   \n",
       "4   4503842027558210  {'visible': {'type': 0, 'list_id': 0}, 'create...   \n",
       "5   4503877749088959  {'visible': {'type': 0, 'list_id': 0}, 'create...   \n",
       "6   4503825493524912  {'visible': {'type': 0, 'list_id': 0}, 'create...   \n",
       "..               ...                                                ...   \n",
       "22  4503823777926509  {'visible': {'type': 0, 'list_id': 0}, 'create...   \n",
       "23  4503823655872375  {'visible': {'type': 0, 'list_id': 0}, 'create...   \n",
       "35  4503823877728490  {'visible': {'type': 0, 'list_id': 0}, 'create...   \n",
       "37  4503823655872375  {'visible': {'type': 0, 'list_id': 0}, 'create...   \n",
       "40  4503823777926509  {'visible': {'type': 0, 'list_id': 0}, 'create...   \n",
       "\n",
       "                                                 text  \\\n",
       "2                     回复@一块废柄:那没事了[允悲][允悲]可能是我没见到吧，我的   \n",
       "3   回复@我家狗子叫小猫:谁强迫她了？恩和开始说的也只是陈述句“孩子随父姓”，现在整的我以为有拿...   \n",
       "4   回复@喷火的龙之传人:张小美那青春又美丽的脸被我颜射了？我自己也彻底懵逼了。而接下来我要面对...   \n",
       "5   回复@脾气暴躁的乐乐:我又不结婚，冠姓权本来也不关我事儿？女权本来就是喜欢就来，不爽就走，又...   \n",
       "6                             回复@总有一天我也会有猫的:可惜他们看不到这些   \n",
       "..                                                ...   \n",
       "22  回复@爱足球的小辛:为什么打在兄弟上，没有姐妹， 为什么是公屏，不是母屏，恶臭蝻权就是你吧[...   \n",
       "23  微博上的\"女权主义\": 你这个女人怎么跟我的观念不一样，我要为女性争取权利，我要先把你这头母...   \n",
       "35                              回复@红尘陌上097:卑鄙是卑鄙者的通行证   \n",
       "37  回复@鹧鸪孤鶱:对啊，现在网络喷子质量堪忧啊  哈哈哈哈哈哈 搞不懂人家生孩子随谁姓关他们啥...   \n",
       "40        回复@爱足球的小辛:保护们把乱拳打在兄弟身上，别被⭕️法家抢了人头[笑哈哈][笑哈哈]   \n",
       "\n",
       "                                                 user         username  \\\n",
       "2   {'id': 5690080210, 'idstr': '5690080210', 'cla...           ZWWzwc   \n",
       "3   {'id': 2274183742, 'idstr': '2274183742', 'cla...              青荼蘼   \n",
       "4   {'id': 1711692274, 'idstr': '1711692274', 'cla...         曲别流殇_898   \n",
       "5   {'id': 2274183742, 'idstr': '2274183742', 'cla...              青荼蘼   \n",
       "6   {'id': 7183137389, 'idstr': '7183137389', 'cla...           孝道将人奴化   \n",
       "..                                                ...              ...   \n",
       "22  {'id': 2117684405, 'idstr': '2117684405', 'cla...         Digital曦   \n",
       "23  {'id': 7189228727, 'idstr': '7189228727', 'cla...       炒饭大师199402   \n",
       "35  {'id': 2426432731, 'idstr': '2426432731', 'cla...  Artemis婷婷_貂爷家皮草   \n",
       "37  {'id': 6130736773, 'idstr': '6130736773', 'cla...       肉肉肉肉啊08256   \n",
       "40  {'id': 2179439900, 'idstr': '2179439900', 'cla...       炒饭大师199402   \n",
       "\n",
       "     replyname  \n",
       "2         一块废柄  \n",
       "3      我家狗子叫小猫  \n",
       "4      喷火的龙之传人  \n",
       "5      脾气暴躁的乐乐  \n",
       "6   总有一天我也会有猫的  \n",
       "..         ...  \n",
       "22      爱足球的小辛  \n",
       "23        鹧鸪孤鶱  \n",
       "35     红尘陌上097  \n",
       "37        鹧鸪孤鶱  \n",
       "40      爱足球的小辛  \n",
       "\n",
       "[537 rows x 18 columns]"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#看看对不对\n",
    "#username是发评论的用户\n",
    "#replyname是他们回复的评论的用户\n",
    "result[-result['reply_comment'].isnull()]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "OK下一步来可视化"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可视化要的数据是两部分:\n",
    ">1.一部分是从这个微博的发布者到评论者的关系，以评论数量为关系的权重  \n",
    "2.第二部分是评论者与评论者之间的关系，以互相回复的数量为关系的权重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T16:19:34.725189Z",
     "start_time": "2020-05-12T16:19:34.708259Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>O</th>\n",
       "      <th>D</th>\n",
       "      <th>count</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>观察者网</td>\n",
       "      <td>1701__</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>观察者网</td>\n",
       "      <td>April1386</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>观察者网</td>\n",
       "      <td>Artemis婷婷_貂爷家皮草</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>观察者网</td>\n",
       "      <td>Digital曦</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>观察者网</td>\n",
       "      <td>GeraIt_Of_Rivia</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      O                D  count\n",
       "0  观察者网           1701__      2\n",
       "1  观察者网        April1386      9\n",
       "2  观察者网  Artemis婷婷_貂爷家皮草      2\n",
       "3  观察者网         Digital曦      5\n",
       "4  观察者网  GeraIt_Of_Rivia      3"
      ]
     },
     "execution_count": 171,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#第一部分\n",
    "datatoplot1 = result[result['replyname'].isnull()].groupby(['username'])['mid'].count().reset_index()\n",
    "datatoplot1.columns = ['D','count']\n",
    "datatoplot1['O'] = '观察者网'\n",
    "datatoplot1[['O','D','count']].head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T16:19:39.857460Z",
     "start_time": "2020-05-12T16:19:39.844497Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>O</th>\n",
       "      <th>D</th>\n",
       "      <th>count</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>April1386</td>\n",
       "      <td>Digital曦</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>April1386</td>\n",
       "      <td>千玺的朱砂痣</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>April1386</td>\n",
       "      <td>想个名字真麻烦23333</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>April1386</td>\n",
       "      <td>我给老板画大饼</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>April1386</td>\n",
       "      <td>樱语嫣然1996</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           O             D  count\n",
       "0  April1386      Digital曦      2\n",
       "1  April1386        千玺的朱砂痣      1\n",
       "2  April1386  想个名字真麻烦23333      1\n",
       "3  April1386       我给老板画大饼      2\n",
       "4  April1386      樱语嫣然1996      1"
      ]
     },
     "execution_count": 172,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#第二部分\n",
    "datatoplot2 = result.groupby(['username','replyname'])['mid'].count().reset_index()\n",
    "datatoplot2.columns = ['O','D','count']\n",
    "datatoplot2.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T16:19:40.897679Z",
     "start_time": "2020-05-12T16:19:40.887707Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:2: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version\n",
      "of pandas will change to not sort by default.\n",
      "\n",
      "To accept the future behavior, pass 'sort=False'.\n",
      "\n",
      "To retain the current behavior and silence the warning, pass 'sort=True'.\n",
      "\n",
      "  \n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>D</th>\n",
       "      <th>O</th>\n",
       "      <th>count</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>1701__</td>\n",
       "      <td>观察者网</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>April1386</td>\n",
       "      <td>观察者网</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>Artemis婷婷_貂爷家皮草</td>\n",
       "      <td>观察者网</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>Digital曦</td>\n",
       "      <td>观察者网</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>GeraIt_Of_Rivia</td>\n",
       "      <td>观察者网</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 D     O  count\n",
       "0           1701__  观察者网      2\n",
       "1        April1386  观察者网      9\n",
       "2  Artemis婷婷_貂爷家皮草  观察者网      2\n",
       "3         Digital曦  观察者网      5\n",
       "4  GeraIt_Of_Rivia  观察者网      3"
      ]
     },
     "execution_count": 173,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#合体！\n",
    "link = pd.concat([datatoplot1,datatoplot2])\n",
    "link.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T16:19:43.963508Z",
     "start_time": "2020-05-12T16:19:43.951514Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>O</th>\n",
       "      <th>count</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>73</td>\n",
       "      <td>观察者网</td>\n",
       "      <td>292</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>57</td>\n",
       "      <td>炒饭大师199402</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>80</td>\n",
       "      <td>青荼蘼</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>34</td>\n",
       "      <td>太座猛于虎</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>27</td>\n",
       "      <td>十四个夏</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             O  count\n",
       "73        观察者网    292\n",
       "57  炒饭大师199402     78\n",
       "80         青荼蘼     27\n",
       "34       太座猛于虎     27\n",
       "27        十四个夏     21"
      ]
     },
     "execution_count": 174,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#然后就是，算一下每个人的发言频次！\n",
    "node = link.groupby(['O'])['count'].sum().reset_index().sort_values(by = 'count',ascending = False)\n",
    "node.head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 可视化"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T15:42:17.519918Z",
     "start_time": "2020-05-12T15:42:17.514933Z"
    }
   },
   "source": [
    "这回我们用到的是pyecharts里面的[Graph关系图](https://pyecharts.org/#/zh-cn/basic_charts?id=graph%ef%bc%9a%e5%85%b3%e7%b3%bb%e5%9b%be)！\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T15:46:21.683820Z",
     "start_time": "2020-05-12T15:46:21.678835Z"
    }
   },
   "source": [
    "查阅echarts的配置项手册可以看到需要的数据格式[graph的配置项手册](https://echarts.apache.org/zh/option.html#series-graph.data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 175,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T16:19:50.170881Z",
     "start_time": "2020-05-12T16:19:50.163901Z"
    }
   },
   "outputs": [],
   "source": [
    "#把node数据整理一下\n",
    "node_json = list(node.apply(lambda r:{'name':r['O'],'value':r['count']},axis = 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T16:19:50.888960Z",
     "start_time": "2020-05-12T16:19:50.867020Z"
    }
   },
   "outputs": [],
   "source": [
    "#把link数据整理一下\n",
    "link_json = list(link.apply(lambda r:{'source':r['O'],'target':r['D']},axis = 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T16:19:53.621651Z",
     "start_time": "2020-05-12T16:19:53.601707Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\pythonscript\\\\2020\\\\pygeo-tutorial\\\\微博评论关系图.html'"
      ]
     },
     "execution_count": 177,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#pyecharts，走起！\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Graph\n",
    "c = (\n",
    "    Graph()\n",
    "    .add(\"weibo\", \n",
    "         nodes = node_json, \n",
    "         links = link_json\n",
    "        )\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title='微博评论关系图')\n",
    "    )\n",
    ")\n",
    "c.render('微博评论关系图.html')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "效果如下\n",
    "<img src=\"https://gitee.com/ni1o1/pygeo-tutorial/raw/master/resource/weibo3.png\"  style=\"width:600px\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T16:21:35.821658Z",
     "start_time": "2020-05-12T16:21:35.794694Z"
    }
   },
   "outputs": [],
   "source": [
    "#把node数据整理一下,加上点的大小\n",
    "node_json = list(node.apply(lambda r:{'name':r['O'],'value':r['count'],'symbolSize':r['count']**0.5},axis = 1))\n",
    "#把link数据整理一下,加上边的粗细\n",
    "link_json = list(link.apply(lambda r:{'source':r['O'],'target':r['D'],'lineStyle':{'width':0.5*r['count']**0.5}},axis = 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T16:21:37.141091Z",
     "start_time": "2020-05-12T16:21:37.111171Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\pythonscript\\\\2020\\\\pygeo-tutorial\\\\微博评论关系图.html'"
      ]
     },
     "execution_count": 179,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#pyecharts，走起！\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Graph\n",
    "c = (\n",
    "    Graph()\n",
    "    .add(\"weibo\", \n",
    "         nodes = node_json, \n",
    "         links = link_json\n",
    "        )\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title='微博评论关系图')\n",
    "    )\n",
    ")\n",
    "c.render('微博评论关系图.html')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-05-12T16:22:55.565311Z",
     "start_time": "2020-05-12T16:22:55.560326Z"
    }
   },
   "source": [
    "效果如下\n",
    "<img src=\"https://gitee.com/ni1o1/pygeo-tutorial/raw/master/resource/weibo4.png\"  style=\"width:600px\">"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
